package com.bijava.util;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by ljj on 2018/6/4.
 */
public class SqlTypeUtil {
    private static final Map<String,String> typeMap = new HashMap<>();

    static {
        typeMap.put("ARRAY",Object.class.getName());
        typeMap.put("BIGINT", Long.class.getName());
        typeMap.put("BINARY", "byte[]"); //$NON-NLS-1$
        typeMap.put("BIT", Boolean.class.getName());
        typeMap.put("BLOB", "BLOB"); //$NON-NLS-1$
        typeMap.put("BOOLEAN", Boolean.class.getName());
        typeMap.put("CHAR", String.class.getName());
        typeMap.put("CLOB", String.class.getName());
        typeMap.put("DATALINK",Object.class.getName());
        typeMap.put("DATE", Date.class.getName());
        typeMap.put("DECIMAL", BigDecimal.class.getName());
        typeMap.put("DISTINCT", Object.class.getName());
        typeMap.put("DOUBLE", Double.class.getName());
        typeMap.put("FLOAT", Double.class.getName());
        typeMap.put("INTEGER", Integer.class.getName());
        typeMap.put("JAVA_OBJECT", Object.class.getName());
        typeMap.put("LONGNVARCHAR", String.class.getName());
        typeMap.put("LONGVARBINARY","byte[]");
        typeMap.put("LONGVARCHAR", String.class.getName());
        typeMap.put("NCHAR", String.class.getName());
        typeMap.put("NCLOB",String.class.getName());
        typeMap.put("NVARCHAR", String.class.getName());
        typeMap.put("NULL", Object.class.getName());
        typeMap.put("NUMERIC", BigDecimal.class.getName());
        typeMap.put("OTHER",Object.class.getName());
        typeMap.put("REAL", Float.class.getName());
        typeMap.put("REF",Object.class.getName());
        typeMap.put("SMALLINT", Short.class.getName());
        typeMap.put("STRUCT", Object.class.getName());
        typeMap.put("TIME", Date.class.getName());
        typeMap.put("TIMESTAMP", Date.class.getName());
        typeMap.put("TINYINT", Byte.class.getName());
        typeMap.put("VARBINARY", "byte[]");
        typeMap.put("VARCHAR", String.class.getName());
        typeMap.put("VARCHAR2", String.class.getName());
    }

    public static String mysqlTypeToJavaType(String sqlType) {
        if (sqlType.equalsIgnoreCase("bit")) {
            return "Boolean";
        } else if (sqlType.equalsIgnoreCase("tinyint")) {
            return "Byte";
        } else if (sqlType.equalsIgnoreCase("smallint")) {
            return "Short";
        } else if (sqlType.equalsIgnoreCase("int")) {
            return "Integer";
        } else if (sqlType.equalsIgnoreCase("bigint")) {
            return "Long";
        } else if (sqlType.equalsIgnoreCase("float")) {
            return "Float";
        } else if (sqlType.equalsIgnoreCase("decimal") || sqlType.equalsIgnoreCase("numeric")
                || sqlType.equalsIgnoreCase("real") || sqlType.equalsIgnoreCase("money")
                || sqlType.equalsIgnoreCase("smallmoney")) {
            return "Double";
        } else if (sqlType.equalsIgnoreCase("varchar") || sqlType.equalsIgnoreCase("char")
                || sqlType.equalsIgnoreCase("nvarchar") || sqlType.equalsIgnoreCase("nchar")
                || sqlType.equalsIgnoreCase("text")) {
            return "String";
        } else if (sqlType.equalsIgnoreCase("datetime") || sqlType.equalsIgnoreCase("timestamp")||sqlType.equalsIgnoreCase("date")) {
            return "Date";
        } else if (sqlType.equalsIgnoreCase("image") || sqlType.equalsIgnoreCase("blob")) {
            return "Blob";
        }
        return "Object";

    }



    public static String oracleSqlTypeToJavaType(String sqlType, Integer dataLength, Integer dataScale) {
        sqlType=sqlType.toUpperCase();
        if ("BIT".equals(sqlType)){
            return calculateBitReplacement(dataLength);
        }else if ("NUMBER".equals(sqlType)){
            return calculateBigDecimalReplacement(dataLength, dataScale);
        }else {
            return typeMap.get(sqlType);
        }
    }



    public static String sqlTypeToMyBaitSqlType(String sqlType) {
        if (sqlType.equalsIgnoreCase("CHAR")){
            return "CHAR";
        }
        if (sqlType.equalsIgnoreCase("NCHAR")){
            return "NCHAR";
        }
        if(sqlType.equalsIgnoreCase("VARCHAR")
                ||sqlType.equalsIgnoreCase("VARCHAR2")
                ||sqlType.equalsIgnoreCase("LONG")
                ||sqlType.equalsIgnoreCase("NVARCHAR")
                ||sqlType.equalsIgnoreCase("NVARCHAR2")){
            return "VARCHAR";
        }
        if (sqlType.equalsIgnoreCase("NUMERIC")||sqlType.equalsIgnoreCase("NUMBER")){
            return "NUMERIC";
        }
        if(sqlType.equalsIgnoreCase("BIT")){
            return "BIT";
        }
        if(sqlType.equalsIgnoreCase("SMALLINT")){
            return "SMALLINT";
        }
        if(sqlType.equalsIgnoreCase("BIGINT")){
            return "BIGINT";
        }
        if(sqlType.equalsIgnoreCase("DECIMAL")){
            return "DECIMAL";
        }
        if(sqlType.equalsIgnoreCase("DOUBLE")){
            return "DOUBLE";
        }
        if(sqlType.equalsIgnoreCase("FLOAT")){
            return "FLOAT";
        }
        if(sqlType.equalsIgnoreCase("INTEGER")){
            return "INTEGER";
        }
        if(sqlType.equalsIgnoreCase("CLOB")||sqlType.equalsIgnoreCase("TEXT")){
            return "CLOB";
        }
        if(sqlType.equalsIgnoreCase("LONG VARCHAR")){
            return "LONGVARCHAR";
        }
        if(sqlType.equalsIgnoreCase("TIME")){
            return "TIME";
        }
        if(sqlType.equalsIgnoreCase("TIMESTAMP")||sqlType.equalsIgnoreCase("DATETIME")){
            return "TIMESTAMP";
        }
        if (sqlType.equalsIgnoreCase("DATE")){
            return "DATE";
        }
        if (sqlType.equalsIgnoreCase("BLOB")){
            return "BLOB";
        }
        return "OTHER";
    }

    private static String calculateBigDecimalReplacement(Integer dataLength,Integer dataScale) {
        if (dataScale > 0 || dataLength > 18 ) {
            return BigDecimal.class.getName();
        } else if (dataLength > 9) {
            return Long.class.getName();
        } else if (dataLength > 4) {
            return (Integer.class.getName());
        } else {
            return (Short.class.getName());
        }
    }
    private static String calculateBitReplacement(Integer dataLength) {
        if (dataLength> 1) {
            return "byte[]";
        } else {
            return Boolean.class.getName();
        }

    }

    public static void main(String[] args) {
        System.out.println(Date.class.getName());
    }

}
